﻿using SmallSpider.Dal;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SmallSpider.Class
{

    public class SpiderThread
    {
        #region << 属性和成员变量 >>

        private Dictionary<string, Thread> arraySpiderThreads;
        private Spider[] arraySpiders;
        public int ThreadCount = 5;
        public static bool ThreadRun = true;
        string _ToUrl;

        public string ToUrl
        {
            get { return _ToUrl; }
            set { _ToUrl = value; }
        }

        private string _UrlSuffix;

        public string UrlSuffix
        {
            get { return _UrlSuffix; }
            set { _UrlSuffix = value; }
        }

        #endregion

        public delegate void ReceiveDataHandler(string strThreadName, string strOutPut);

        public event ReceiveDataHandler ReceivedData;

        public delegate void ThreadFinishHandler(string strThreadName);

        public event ThreadFinishHandler ThreadFinish;

        public SpiderThread()
        {
            arraySpiders = new Spider[ThreadCount];
            arraySpiderThreads = new Dictionary<string, Thread>();
            for (int i = 0; i < ThreadCount; i++)
            {
                arraySpiders[i] = new Spider();
            }
            for (int i = 0; i < ThreadCount; i++)
            {
                arraySpiders[i].ReceivedData += new Spider.ReceiveDataHandler(_Spider_ReceivedData);
                arraySpiders[i].SpiderStop += SpiderThread_SpiderStop;
            }
        }

        private void SpiderThread_SpiderStop(string strThreadName)
        {
            ThreadFinish(strThreadName);
        }

        private void _Spider_ReceivedData(string strThreadName, string strUrl, string strReceiveData, string strCharset)
        {
            string strTitle = string.Empty;
            string strKeywords = string.Empty;
            string strDescription = string.Empty;
            HttpAnalysis _HttpAnalysis = new HttpAnalysis(strReceiveData);
            strTitle = _HttpAnalysis.GetHtmlTitle();
            strKeywords = _HttpAnalysis.GetHtmlKeywords();
            strDescription = _HttpAnalysis.GetHtmlDescription();
            int intWebSiteId = new DAL_WebSite().InsertWebSite(strTitle, strUrl,
                                                strKeywords, strDescription, strCharset);
            //List<Link> allLink = _HttpAnalysis.GetAllLink(strReceiveData); 
            ReceivedData(strThreadName, strThreadName + "：" + strUrl);
        }

        public void Start()
        {
            if (!string.IsNullOrEmpty(ToUrl))
            {
                for (int i = 0; i < ThreadCount; i++)
                {
                    arraySpiders[i].UrlSuffix = _UrlSuffix;
                    arraySpiders[i].ToUrl = ToUrl;/*指定URL*/
                }
            }
            if (arraySpiderThreads.Count != ThreadCount)
            {
                for (int i = 0; i < ThreadCount; i++)
                {
                    arraySpiderThreads.Add(i.ToString(), new Thread(new ThreadStart(arraySpiders[i].Predation)));
                }
                for (int i = 0; i < ThreadCount; i++)
                {
                    arraySpiderThreads[i.ToString()].Name = "Thread" + i.ToString();
                }
            }
            if (arraySpiderThreads.Count > 0)
            {
                for (int i = 0; i < ThreadCount; i++)
                {
                    arraySpiderThreads[i.ToString()].Start();
                }
            }
        }

        public void Stop()
        {
            ThreadRun = false;
        }
    }
}
